package group2assignone.view.administrator;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Administrator;
import group2assignone.model.Manager;
import group2assignone.ucm.administrator.*;
import group2assignone.ucm.UCLogout;
import group2assignone.view.OurView;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *public class AdminMainView extends OurView
 * this is the main administrator view
 * it shows a listing of all managers currently in the database
 * @author group2
 */
public class AdminMainView extends OurView {

    private JButton logoutButton = new JButton("Logout");
    private JButton editButton = new JButton("Edit");
    private JButton addNewButton = new JButton("Add Manager");
    private JButton deleteButton = new JButton("Delete");
    private JList manList = new JList();
    private Manager model;
    private Administrator admin;
    private ManagerTable table;
    private JTable manTable;
    private JScrollPane tableScroll = new JScrollPane();
    private int selectedRow;

    /**
     * sets the manager table
     * @param managerList
     */
    public AdminMainView(ArrayList<Manager> managerList, Administrator admin) {
        table = new ManagerTable(managerList);
        this.admin = admin;
    }

    @Override
    public void addComponents() {
        setTitle("Administrator View");

        manTable = new JTable(table);
        manTable.setRowSelectionAllowed(true);
        manTable.setColumnSelectionAllowed(false);
        manTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableScroll = (new JScrollPane(manTable));
        centerPanel.add(tableScroll);

        northPanel.add(logoutButton);
        southPanel.add(addNewButton);
        southPanel.add(editButton);
        southPanel.add(deleteButton);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        addNewButton.addActionListener(new UCCtoALAdapter(new UCUIAddManager(this)));
        editButton.addActionListener(new UCCtoALAdapter(new UCEditManager(this)));
        deleteButton.addActionListener(new UCCtoALAdapter(new UCDeleteManager(this)));
        logoutButton.addActionListener(new UCCtoALAdapter(new UCLogout(this, admin)));
        manTable.getSelectionModel().addListSelectionListener(new RowListener());

        selectedRow = manTable.getSelectedRow();
    }

    /**
     * This function returns a boolean result if there is a selected manager
     * @return
     * return true if is a selection
     * return false if there is no selection
     */
    public boolean isNoSelection() {
        return manList.isSelectionEmpty();
    }

    /**
     * this function returns the selected row number
     * this is to enable deleting and editing of the correct table row
     * @return
     */
    public String[] getRow() {
        String[] row = new String[manTable.getColumnCount()];
        int column = 0;

        for (String s : row) {
            System.out.println("col = " + column);
            // s.equals( table.getValueAt(selectedRow, column));
            row[column] = table.getValueAt(selectedRow, column);
            column++;
        }
        return row;

    }

    /**
     * This function returns the string entered into the textfield Username
     * @return
     */
    public String getUserName() {
        int column = 0;
        String userName = table.getValueAt(selectedRow, column);
        return userName;
    }

    /**
     * This function is to set the selected row in program
     * @param row
     */
    public void setSelectedRow(int row) {
        selectedRow = row;

    }

    /**
     * this function is to return the private variable which holds the selected row
     * @return
     */
    public int getSelectedRow() {
        return selectedRow;
    }

    private class RowListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent arg0) {

            setSelectedRow(manTable.getSelectedRow());
        }
    }
}
